
#pragma once

// Java => C
#define BEGIN_NATIVE_METHODS() \
    static void RegisterNativeMethods(JNIEnv* env, jclass clazz) { \
        printf("register native methods for class: %s\n", getClassName()); \
        JNINativeMethod methods[] = {
#define ON_NATIVE_METHOD(m, s, f)  {(char*)m, (char*)s, (void*)f},
#define END_NATIVE_METHODS() \
    }; \
    if (sizeof(methods) > 0) \
        env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); \
    }
 
// C => Java
#define BEGIN_JAVA_METHODS() \
    virtual void initJavaMethods(JNIEnv* env) { \
        char classname[1024]; \
        strncpy(classname, getClassName(), sizeof(classname)); \
        JniObject::alterClassName(classname); \
        printf("register java methods for class: %s(instance = %p)\n", classname, this); \
        jclass mClass = (jclass)(env->FindClass(classname)); \
        if (!mClass) { \
            printf("class not found: %s\n", classname); \
            return; \
        }
#define ON_JAVA_METHOD(method, n, s) method = env->GetMethodID(mClass, n, s);
#define ON_STATIC_JAVA_METHOD(method, n, s) method = env->GetStaticMethodID(mClass, n, s);
#define END_JAVA_METHODS() }


// #define EMPTY_NATIVE_METHODS static void RegisterNativeMethods(JNIEnv* env, jclass clazz) {}
// #define EMPTY_JAVA_METHODS()  virtual void initJavaMethods(JNIEnv* env) {}

#define DECLARE_CLASS_NAME(className) \
    static const char * getClassName() { return className; } \
    virtual const char * getCurrentClassName() { return className; }

#define REGISTER_CLASS_CREATOR(classname) \
    class classname ## FactoryHelper : public JniObjectFactory { \
    public: \
            classname ## FactoryHelper() { reg(classname::getClassName()); } \
        virtual JniObject* doCreate() { return new classname(); } \
    }; \
    classname ## FactoryHelper    jniClass ## classname ## CreatorFactory ## __LINE__; \
    JniObjectNativeMethodHelper jniClass ## classname ## NativeRegister ## __LINE__(classname::RegisterNativeMethods, classname::getClassName());


